package com.sskj.givecoin.v.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.widget.ToolBarLayout;
import com.sskj.givecoin.p.activity.VerifyGuidePresenter;
import com.sskj.givecoin.utils.FileUtil;
import com.sskj.givecoin.utils.PictureUtil;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.base.BaseActivity;

import java.io.File;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class VerifyGuideActivity extends BaseActivity<VerifyGuidePresenter> {
    private static final int REQUEST_A_CAMERA = 1000;
    private static final int REQUEST_B_CAMERA = 1001;
    private static final int REQUEST_C_CAMERA = 1002;
    private static final String NAME_A = "photo_a.jpg";
    private static final String NAME_B = "photo_b.jpg";
    private static final String NAME_C = "photo_c.jpg";
    private String mCurrFileName;
    private HashMap<String, Object> mParams = new HashMap<String, Object>();

    @BindView(R.id.toolBar)
    ToolBarLayout toolBar;
    @BindView(R.id.verify_submit)
    Button mVerifySubmit;
    @BindView(R.id.verify_photo_a)
    ImageView mVerifyPhotoA;
    @BindView(R.id.verify_photo_b)
    ImageView mVerifyPhotoB;
    @BindView(R.id.verify_photo_c)
    ImageView mVerifyPhotoC;
    /**
     * 实名认证
     * startActForResult requestCode
     */
    public static final int VERIFY_IDENT_CODE = 1103;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_verify_guide;
    }

    @Override
    public VerifyGuidePresenter getPresenter() {
        return new VerifyGuidePresenter();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, VerifyGuideActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        toolBar.setLeftButtonOnClickLinster(v -> {
            finish();
        });
    }

    @Override
    protected void onViewBinding() {
        super.onViewBinding();
        toolBar.setTitle(App.INSTANCE.getString(R.string.strVerifyGuideActivity203));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            String filePath = null;
            switch (requestCode) {
                case REQUEST_A_CAMERA://人证合一
                    filePath = new File(getFilesDir(), NAME_A).getAbsolutePath();
                    break;
                case REQUEST_B_CAMERA://身份证正面
                    filePath = new File(getFilesDir(), NAME_B).getAbsolutePath();
                    break;
                case REQUEST_C_CAMERA: //身份证背面
                    filePath = new File(getFilesDir(), NAME_C).getAbsolutePath();
                    break;
            }
            if (TextUtils.isEmpty(filePath)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyGuideActivity204));
                return;
            }

            Bitmap smallBitmap = PictureUtil.getSmallBitmap(filePath);
            PictureUtil.saveBitmap(smallBitmap, 70, filePath, Bitmap.CompressFormat.JPEG);
            File file = new File(filePath);
            if (file != null && smallBitmap != null) {
                setFilePath(smallBitmap, filePath, file);
                mPresenter.submitVerify(file);
                if (mParams.get("photo_a") != null && mParams.get("photo_b") != null && mParams.get("photo_b") != null) {
                    mVerifySubmit.setEnabled(true);
                }
            }
        }

    }

    private void setFilePath(Bitmap smallBitmap, String filePath, File file) {
        if (NAME_A.equals(mCurrFileName)) {
//            mParams.put("photo_a", file);
//            mParams.put("photoAName", mCurrFileName);
            mVerifyPhotoA.setBackground(new BitmapDrawable(smallBitmap));
        } else if (NAME_B.equals(mCurrFileName)) {
//            mParams.put("photo_b", file);
//            mParams.put("photoBName", mCurrFileName);
            mVerifyPhotoB.setBackground(new BitmapDrawable(smallBitmap));

        } else if (NAME_C.equals(mCurrFileName)) {//身份证背面
//            mParams.put("photo_c", file);
//            mParams.put("photoCName", mCurrFileName);
            mVerifyPhotoC.setBackground(new BitmapDrawable(smallBitmap));
        }

    }


    @OnClick({R.id.verify_photo_a, R.id.verify_photo_b, R.id.verify_photo_c, R.id.verify_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.verify_photo_a:
                mCurrFileName = NAME_A;
                Intent intent = new Intent(this, CameraActivity.class);
                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                        FileUtil.getSaveFile(getApplication(), NAME_A).getAbsolutePath());
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);
                startActivityForResult(intent, REQUEST_A_CAMERA);
                break;
            case R.id.verify_photo_b:
                mCurrFileName = NAME_B;
                intent = new Intent(this, CameraActivity.class);
                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication(), NAME_B).getAbsolutePath());
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
                startActivityForResult(intent, REQUEST_B_CAMERA);
                break;
            case R.id.verify_photo_c:
                mCurrFileName = NAME_C;
                intent = new Intent(this, CameraActivity.class);
                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication(), NAME_C).getAbsolutePath());
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
                startActivityForResult(intent, REQUEST_C_CAMERA);
                break;
            case R.id.verify_submit:
                if (mParams.size() < 2) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyGuideActivity214));
                    return;
                }
                mPresenter.submitVerifyEnd(mParams);
                break;
        }
    }

    public void onSubmitVerifyResponseSuccess() {
        ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyGuideActivity215));
//        setResult(RESULT_OK, new Intent().putExtra("resultData", 2));
        finish();
//        startActivity(new Intent(mActivity, CommonActivity.class));
    }

    public void putList(String url) {
        if (NAME_A.equals(mCurrFileName)) {
            mParams.put("photoAName", url);
        } else if (NAME_B.equals(mCurrFileName)) {
            mParams.put("photoBName", url);
        } else if (NAME_C.equals(mCurrFileName)) {//身份证背面
            mParams.put("photoCName", url);
        }
    }
}
